package com.zx.oa.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;

//@WebFilter(urlPatterns = "/jsp/*.jsp")
@WebFilter(urlPatterns = "*.jsp")
public class LoginCheckFilter  implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        HttpSession session= request.getSession();
        ArrayList allowPathList = new  ArrayList();
        //需要放行的页面
        allowPathList.add("/login.jsp");
//        allowPathList.add("/login.do");

        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        String currentUrl = httpRequest.getServletPath();

        if (session.getAttribute("loginName")==null&&!allowPathList.contains(currentUrl)) {
            //在过滤器中判断如果没有登录，并且请求是不包含在放行的请求中的
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
        filterChain.doFilter(request,response);
    }


}
